package com.jm.hosp.controller.api;

import com.jm.common.exception.YyghException;
import com.jm.common.helper.HttpRequestHelper;
import com.jm.common.result.Result;
import com.jm.common.result.ResultCodeEnum;
import com.jm.common.utils.MD5;
import com.jm.hosp.service.DepartmentService;
import com.jm.hosp.service.HospitalService;
import com.jm.hosp.service.HospitalSetService;
import com.jm.hosp.service.ScheduleService;
import com.jm.model.hosp.Department;
import com.jm.model.hosp.Hospital;
import com.jm.model.hosp.Schedule;
import com.jm.vo.hosp.DepartmentQueryVo;
import com.jm.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Resource
    private HospitalSetService hospitalSetService;  // 医院设置
    @Resource
    private HospitalService hospitalService;        // 医院
    @Resource
    private DepartmentService departmentService;    // 科室
    @Resource
    private ScheduleService scheduleService;        // 排班

    //删除排班接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取传递过来的参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if (!this.isSignEqual(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //调用service层方法，删除科室接口
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    //查看排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递过来的参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if (!this.isSignEqual(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //分页条件
        Integer page = Integer.parseInt((String) paramMap.get("page"));
        Integer limit = Integer.parseInt((String) paramMap.get("limit")) ;

        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode((String) paramMap.get("hoscode"));
        //调用service层方法，查询科室信息
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if (!this.isSignEqual(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来的参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if (!this.isSignEqual(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //调用service层方法，删除科室接口
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递过来的参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if (!this.isSignEqual(paramMap)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //分页条件
        Integer page = Integer.parseInt((String) paramMap.get("page"));
        Integer limit = Integer.parseInt((String) paramMap.get("limit")) ;

        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode((String) paramMap.get("hoscode"));
        //调用service层方法，查询科室信息
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if(!this.isSignEqual(paramMap)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(paramMap);
        return Result.ok();
    }

    //查询医院接口
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if(!this.isSignEqual(paramMap)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode((String) paramMap.get("hoscode"));
        return Result.ok(hospital);
    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名验证
        if(!this.isSignEqual(paramMap)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中 "+"转换为了" ",转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    //判断签名是否一致
    private boolean isSignEqual(Map<String, Object> paramMap){
        //获取医院编号
        String hoscode= (String) paramMap.get("hoscode");
        //1.获取管理系统传递过来的签名，签名已被MD5加密
        String sign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编号，查询数据库，查询表名
        String signKey=hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        return signKeyMD5.equals(sign);
    }
}
